package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import com.oocl.dao.OrdersDao;
import com.oocl.pojo.Orders;
import com.oocl.util.DBUtil;

public class OrdersDaoImpl implements OrdersDao{

	public int insertMore(List<Orders> os) {
		String sql = "insert into orders(id,p_id,b_id,b_date,count) values(?,?,?,?,?)";
		int count = -1;
		Connection con = null;
		PreparedStatement stat = null;
		try {
			con = DBUtil.createConnection();
			con.setAutoCommit(false);
			stat = con.prepareStatement(sql);
			for (Orders o : os) {
				stat.setString(1, o.getId());
				stat.setString(2, o.getpId());
				stat.setString(3, o.getbId());
				stat.setDate(4, new Date(o.getbDate().getTime()));
				stat.setInt(5, o.getCount());
				stat.addBatch();
			}
			int[] counts = stat.executeBatch();
			count = counts.length;
			con.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return count;
	}

}
